#include <stdio.h>
#include <math.h>

void findPerfectNumbers(int n);

int main()
{
    int n;

    printf("请输入一个正整数n：");
    if (scanf("%d", &n) != 1 || n <= 0)
    {
        printf("输入无效\n");
        return 1;
    }

    findPerfectNumbers(n);
    return 0;
}

void findPerfectNumbers(int n)
{
    int count = 0;

    printf("1~%d之间的完数有：", n);

    for (int num = 2; num <= n; num++)
    {
        int sum = 1;

        for (int i = 2; i * i <= num; i++)
        {
            if (num % i == 0)
            {
                sum += i;
                if (i != num / i)
                {
                    sum += num / i;
                }
            }
        }

        if (sum == num)
        {
            printf("%d ", num);
            count++;
        }
    }

    if (count == 0)
    {
        printf("无");
    }

    printf("\n共有%d个完数\n", count);
}